namespace TheCoinMaster
{
    using Styx;
    using Styx.Combat.CombatRoutine;
    using Styx.Helpers;
    using Styx.Logic;
    using Styx.Logic.Combat;
    using Styx.Logic.Inventory;
    using Styx.Logic.Inventory.Frames.Gossip;
    using Styx.Logic.Pathing;
    using Styx.Logic.Profiles;
    using Styx.Plugins.PluginClass;
    using Styx.WoWInternals;
    using Styx.WoWInternals.WoWObjects;

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;
    using System.Xml.Linq;

   public class TheCoinMaster : HBPlugin
   {
      public override string Name { get { return "The Coin Master Fishing"; } }
      public override string Author { get { return "Pasterke"; } }
      public override Version Version { get { return _version; } }
      private readonly Version _version = new Version(1, 0, 0, 0);
      

      private List<uint> _itemList = new List<uint>()
      {
		 43571, // Sewer Carp
		 43627, // Gold Coins
		 43628,
		 43629,
		 43630,
		 43631,
		 43632,
		 43633,
		 43634,
		 43635,
		 43636,
		 43637,
		 43638,
		 43639,
		 43640,
		 43641,
		 43642,
		 43643, // Silver Coin
		 43644, // Silver Coin
		 43645, // Bent Fishing Hook
		 43646, // Foutain Goldfish
		 43673, // Silver Coins
		 43674,
		 43675,
		 43676,
		 43677,
		 43678,
		 43679,
		 43680,
		 43681,
		 43682,
		 43683,
		 43684,
		 43685,
		 43686,
		 43687,
		 43694, // Drowned Rat
		 43701,
		 43702,
		 43703,
		 43704,
		 43705,
		 43706,
		 43707,
		 43708,
		 43709,
		 43710,
		 43711,
		 43712,
		 43713,
		 43714,
		 43715,
		 43716,
		 43717,
		 43718,
		 43719,
		 43720,
		 43721,
		 43722,
		 43723,
		 45190, // Driftwood
		 45194, // Tangled Fishing Line
		 45196, // Tattered Cloth
		 45198, // Weeds
		 45200, // Sickly Fish
		 45202, // Water Snail
		 45072  // Noblegarden Chocolate
      };

      private static Stopwatch sw = new Stopwatch();
      private static LocalPlayer Me { get { return ObjectManager.Me; } }
      public override void Pulse()
        {
            if (Battlegrounds.IsInsideBattleground || ObjectManager.Me.Mounted || ObjectManager.Me.Combat)
                return;

            if (!sw.IsRunning)
                sw.Start();

            if (sw.Elapsed.TotalSeconds > 1)
            {
				foreach (WoWItem item in ObjectManager.GetObjectsOfType<WoWItem>())
                {
                    if (_itemList.Contains(item.Entry))
                    {
						if (item != null)
						{
							if(item.BagSlot != -1)
							{
								if (item.Entry == 43571 || item.Entry == 45190 || item.Entry == 45194 || item.Entry == 45196 || item.Entry == 45198 || item.Entry == 45200 || item.Entry == 45202  )
								{
									 
									Styx.Helpers.Logging.Write(Color.FromName("Yellow"), "[TheCoinMaster]: Destroying {0} ", item.Name, item.StackCount);
									item.PickUp();
									//Lua.DoString("PickupContainerItem(" + bagins + "," + bagssl + ")");
									Lua.DoString("DeleteCursorItem()");
									Thread.Sleep(300);
									
								}
							else if (item.Entry >= 43643 && item.Entry <= 43723)
								{
									 
									Styx.Helpers.Logging.Write(Color.FromName("Yellow"), "[TheCoinMaster]: Destroying {0} ", item.Name, item.StackCount);
									 item.PickUp();
									//Lua.DoString("PickupContainerItem(" + bagins + "," + bagssl + ")");
									Lua.DoString("DeleteCursorItem()");
									Thread.Sleep(300);
									
								}
							else if (item.Entry >= 43627 && item.Entry <= 43642 && !Me.Auras.ContainsKey("Lucky"))
								{
									 
									Styx.Helpers.Logging.Write(Color.FromName("Yellow"), "[TheCoinMaster]: Using Gold Coin {0} ", item.Name, item.StackCount);
									Lua.DoString("UseItemByName(\"" + item.Name + "\")");
									Thread.Sleep(500);
									
								}
							/*else
								{
									if (item.StackCount>=1)
										{
											Styx.Helpers.Logging.Write(Color.FromName("DarkRed"), "[TidyBags 3]: Using {0} we have {1}", item.Name, item.StackCount);
											Lua.DoString("UseItemByName(\"" + item.Name + "\")");
										}
								} */
							}
						}
					}
				}
                while (Styx.Logic.LootTargeting.LootFrameIsOpen)
					{
						Thread.Sleep(GetPing * 2 + 100);

						if (sw.Elapsed.TotalSeconds >= 6)
						{
							break;
						}
					}
                sw.Reset();
                sw.Start();
            }
		}
		public static int GetPing
		{
			get
			{
				return Lua.GetReturnVal<int>("return GetNetStats()", 2);
			}
		}
    }
}